package techno.dhr.touchWall;

import java.util.Random;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class TouchColorWallpaper extends WallpaperService {

	public Engine onCreateEngine() {
		return new WallEngine();
	}

	class WallEngine extends Engine{

		int currentTouchX = 100;
		int currentTouchY = 100;

		private final Random rnd = new Random();

		private float mX, mY;
		
		private static final String TAG = "WallEngine";

		SurfaceHolder holder;

		Canvas mCanvas;

		private Paint mPaint;

		Path mPath;

		@Override
		public void onCreate(SurfaceHolder surfaceHolder) {
			super.onCreate(surfaceHolder);
			mPaint = new Paint();
			mPaint.setAntiAlias(true);
			mPaint.setStrokeWidth(4);
			mPaint.setStrokeCap(Paint.Cap.ROUND);
			mPaint.setStyle(Paint.Style.STROKE);
			mPaint.setColor(Color.RED);

			mPath = new Path();

		}

		@Override
		public void onSurfaceCreated(SurfaceHolder holder) {
			super.onSurfaceCreated(holder);
			Log.d(TAG, "ONSURFACECREATED");
			mCanvas = holder.lockCanvas();
			mCanvas.drawColor(Color.BLACK);
			holder.unlockCanvasAndPost(mCanvas);
		}

		@Override
		public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
			super.onSurfaceChanged(holder, format, width, height);
			Log.d(TAG, "ONSURFACECHANGED");
			mCanvas = holder.lockCanvas();
			mCanvas.drawColor(Color.BLACK);
			holder.unlockCanvasAndPost(mCanvas);
		}

		private void touch_start(float x, float y) {
			Log.i(TAG, "TOUCH_START");
			//mPath.reset();
			mPaint.setStrokeWidth(rnd.nextInt(10));
			mPaint.setAlpha(rnd.nextInt(256));
			mPaint.setARGB(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
			mPath.moveTo(x, y);
			mX = x;
			mY = y;
		}
		private void touch_move(float x, float y) {
			Log.i(TAG, "TOUCH_MOVE");
			float dx = Math.abs(x - mX);
			float dy = Math.abs(y - mY);

			mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
			mCanvas.drawPath(mPath, mPaint);

			mX = x;
			mY = y;
		}
		private void touch_up() {
			Log.i(TAG, "TOUCH_UP");
			mPath.lineTo(mX, mY);
			// commit the path to our offscreen
			mCanvas.drawPath(mPath, mPaint);
			// kill this so we don't double draw
			mPath.reset();
		}

		@Override
		public void onTouchEvent(MotionEvent event) {
			super.onTouchEvent(event);

			int action = event.getAction();

			if(action==MotionEvent.ACTION_DOWN || action==MotionEvent.ACTION_MOVE  || action==MotionEvent.ACTION_UP ){
				holder = getSurfaceHolder();
				mCanvas = null;
				mCanvas = holder.lockCanvas();			
			}


			switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:
				Log.d(TAG, "ACTION_DOWN");
				touch_start(event.getX(), event.getY());

				break;
			case MotionEvent.ACTION_MOVE:
				Log.d(TAG, "ACTION_MOVE");
				touch_move(event.getX(), event.getY());
				break;
			case MotionEvent.ACTION_UP:
				Log.d(TAG, "ACTION_UP");
				touch_up();
				break;
			}
			if(action==MotionEvent.ACTION_DOWN || action==MotionEvent.ACTION_MOVE  || action==MotionEvent.ACTION_UP ){
				holder.unlockCanvasAndPost(mCanvas);

			}

		}

		@Override
		public void onSurfaceDestroyed(SurfaceHolder holder) {
			super.onSurfaceDestroyed(holder);
			mPaint = null;
			mPath = null;
			mCanvas = null;
		}

	}
}
